package com.bci.pwtz.controller.app;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bci.pwtz.common.remote.BaseRequest;
import com.bci.pwtz.common.remote.ListResponse;
import com.bci.pwtz.common.remote.ReturnCode;
import com.bci.pwtz.common.remote.req.SensitiveWordReq;
import com.bci.pwtz.controller.AbstractController;
import com.bci.pwtz.exception.PwtzDatabaseException;
import com.bci.pwtz.mysql.model.SensitiveWord;
import com.bci.pwtz.service.SensitiveWordService;

/**
 * 敏感词控制器controller <br/>
 * <p>
 * Description:<br/>
 * <p>
 * 
 * <PRE>
 *      
 * </PRE>
 * <p>
 */
@Controller
@RequestMapping("/api/sensitiveword")
public class SensitiveWordAppController extends AbstractController {
	@Autowired
	private SensitiveWordService sensitiveWordService;
	/*@RequestMapping(value="/list",method=RequestMethod.POST)
	@ResponseBody
	public ListResponse<SensitiveWord> checkWords(HttpServletRequest request){
		SensitiveWordReq sensitiveWordReq=null;
		PaginationList<SensitiveWord> sensitiveWords;
		ListResponse<SensitiveWord> sensitiveWordResp=new ListResponse<SensitiveWord>();
		try{
			sensitiveWordReq=getJsonToObj(request,SensitiveWordReq.class);
			if(sensitiveWordReq==null){
				sensitiveWordResp.setReturnCode(ReturnCode.PARAMETER_ERROR);
				sensitiveWordResp.setReturnMsg("参数错误");
				return sensitiveWordResp;
			}
			if(sensitiveWordReq.getBeginTime()==null||sensitiveWordReq.getPageNo()==null||sensitiveWordReq.getPageSize()==null){
				sensitiveWordResp.setReturnCode(ReturnCode.PARAMETER_ERROR);
				sensitiveWordResp.setReturnMsg("参数错误");
				return sensitiveWordResp;
			}
			if(sensitiveWordReq.getSecretKey()==null){
				sensitiveWordResp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
				sensitiveWordResp.setReturnMsg("密钥为空");
				sensitiveWordResp.setTimestamp(sensitiveWordReq.getTimestamp());
                return sensitiveWordResp;
            }
            if(sensitiveWordReq.getTimestamp()==null){
            	sensitiveWordResp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
            	sensitiveWordResp.setReturnMsg("参数错误");
            	sensitiveWordResp.setTimestamp(sensitiveWordReq.getTimestamp());
                return sensitiveWordResp;
            }
            if(!validateSecretKey(sensitiveWordReq.getSecretKey(), sensitiveWordReq.getTimestamp())){
            	sensitiveWordResp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
            	sensitiveWordResp.setReturnMsg("密钥错误");
            	sensitiveWordResp.setTimestamp(sensitiveWordReq.getTimestamp());
                return sensitiveWordResp;
            }
            String beginTime=sensitiveWordReq.getBeginTime();
            String endTime=sensitiveWordReq.getEndTime();
            int pageNo=sensitiveWordReq.getPageNo();
            int pageSize=sensitiveWordReq.getPageSize();
            sensitiveWords=sensitiveWordService.selectSensitiveWords(beginTime,endTime,pageSize,pageNo);
            if(null!=sensitiveWords){
            	sensitiveWordResp.setReturnCode(ReturnCode.SUCCESS);
            	sensitiveWordResp.setObjs(sensitiveWords);
            	sensitiveWordResp.setReturnMsg("查询成功");
            	sensitiveWordResp.setTimestamp(sensitiveWordReq.getTimestamp());
            }else{
            	sensitiveWordResp.setReturnCode(ReturnCode.INTERFACE_INVOKE_ERROR);
            	sensitiveWordResp.setReturnMsg("调用接口返回失败");
            	sensitiveWordResp.setTimestamp(sensitiveWordReq.getTimestamp());
            }
		}catch(PwtzDatabaseException e){
			sensitiveWordResp.setReturnCode(ReturnCode.DB_EXCEPTION);
			sensitiveWordResp.setReturnMsg("数据库操作异常");
			sensitiveWordResp.setTimestamp(sensitiveWordReq.getTimestamp());
		}catch(Exception e){
			sensitiveWordResp.setReturnCode(ReturnCode.UNKOWN_ERROR);
			sensitiveWordResp.setReturnMsg("系统错误");
			sensitiveWordResp.setTimestamp(sensitiveWordReq.getTimestamp());
		}
		return sensitiveWordResp;
	}*/
	@RequestMapping(value="/list",method=RequestMethod.POST)
    @ResponseBody
    public ListResponse<SensitiveWord> checkWords(HttpServletRequest request){
        BaseRequest sensitiveWordReq=null;
        List<SensitiveWord> sensitiveWords;
        ListResponse<SensitiveWord> sensitiveWordResp=new ListResponse<SensitiveWord>();
        try{
            sensitiveWordReq=getJsonToObj(request,SensitiveWordReq.class);
            if(sensitiveWordReq==null){
                sensitiveWordResp.setReturnCode(ReturnCode.PARAMETER_ERROR);
                sensitiveWordResp.setReturnMsg("参数错误");
                return sensitiveWordResp;
            }
            if(sensitiveWordReq.getSecretKey()==null){
                sensitiveWordResp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
                sensitiveWordResp.setReturnMsg("密钥为空");
                sensitiveWordResp.setTimestamp(sensitiveWordReq.getTimestamp());
                return sensitiveWordResp;
            }
            if(sensitiveWordReq.getTimestamp()==null){
                sensitiveWordResp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
                sensitiveWordResp.setReturnMsg("参数错误");
                sensitiveWordResp.setTimestamp(sensitiveWordReq.getTimestamp());
                return sensitiveWordResp;
            }
            if(!validateSecretKey(sensitiveWordReq.getSecretKey(), sensitiveWordReq.getTimestamp())){
                sensitiveWordResp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
                sensitiveWordResp.setReturnMsg("密钥错误");
                sensitiveWordResp.setTimestamp(sensitiveWordReq.getTimestamp());
                return sensitiveWordResp;
            }
            sensitiveWords=sensitiveWordService.selectSensitiveWordList();
            if(null!=sensitiveWords){
                sensitiveWordResp.setReturnCode(ReturnCode.SUCCESS);
                sensitiveWordResp.setObjss(sensitiveWords);
                sensitiveWordResp.setReturnMsg("查询敏感词汇成功");
                sensitiveWordResp.setTimestamp(sensitiveWordReq.getTimestamp());
            }else{
                sensitiveWordResp.setReturnCode(ReturnCode.INTERFACE_INVOKE_ERROR);
                sensitiveWordResp.setReturnMsg("查询敏感词汇调用接口返回失败");
                sensitiveWordResp.setTimestamp(sensitiveWordReq.getTimestamp());
            }
        }catch(PwtzDatabaseException e){
            sensitiveWordResp.setReturnCode(ReturnCode.DB_EXCEPTION);
            sensitiveWordResp.setReturnMsg("数据库操作异常");
            sensitiveWordResp.setTimestamp(sensitiveWordReq.getTimestamp());
        }catch(Exception e){
            sensitiveWordResp.setReturnCode(ReturnCode.UNKOWN_ERROR);
            sensitiveWordResp.setReturnMsg("系统错误");
            sensitiveWordResp.setTimestamp(sensitiveWordReq.getTimestamp());
        }
        return sensitiveWordResp;
    }
}
